﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PeonyShop.Bussiness;
using System.Web.Services;
using System.Web.Script.Serialization;
using PenoyShop.Utilities;
using PeonyShop.Class;

namespace PeonyShop.Admin
{
    public partial class Images : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Admin"] != null)
            {
                if (!IsPostBack)
                {
                    BACategory bCate = new BACategory();
                    DataSet dsMain = bCate.GetAllCategoryByParenṭ(0, true, false);
                    ddlCate.DataTextField = "Name";
                    ddlCate.DataValueField = "CateId";
                    ddlCate.DataSource = dsMain.Tables[0];
                    ddlCate.DataBind();
                    ddlCate.Items.Insert(0, new ListItem("-- Please Choose --", "-1"));

                    /* For add */
                    ddlCategory.DataTextField = "Name";
                    ddlCategory.DataValueField = "CateId";
                    ddlCategory.DataSource = dsMain.Tables[0];
                    ddlCategory.DataBind();
                    ddlCategory.Items.Insert(0, new ListItem("-- Please Choose --", "-1"));
                }
            }
            else
                CodeHelper.Alert("Login trước rồi làm gì thì làm nhé", "Login.aspx");
        }

        [WebMethod]
        public static string GetSubCate(int CateId)
        {
            BACategory bCate = new BACategory();
            DataSet dsMain = bCate.GetAllCategoryByParenṭ(CateId, true, false);
            CategoryList List = new CategoryList();
            if (dsMain != null)
            {
                DataTable dt = dsMain.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    List.lst = new List<CategoryItem>();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        CategoryItem item = new CategoryItem();
                        item.CateId = dr["CateId"].ToString();
                        item.Name = dr["Name"].ToString();
                        List.lst.Add(item);
                    }
                }
            }
            return new JavaScriptSerializer().Serialize(List);
        }

        [WebMethod]
        public static string GetProductByCate(int MainCate, int SubCate)
        {
            BProduct bProduct = new BProduct();
            DataSet dsPro = bProduct.GetProductByCate(MainCate, SubCate);
            ProductList List = new ProductList();
            if (dsPro != null)
            {
                DataTable dt = dsPro.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    List.Lst = new List<CProduct>();
                    int len = dt.Rows.Count;
                    for (int i = 0; i < len; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        CProduct item = new CProduct();
                        item.Id = Convert.ToInt32(dr["id"]);
                        item.Name = dr["Name"].ToString();
                        item.MainCate = Convert.ToInt32(dr["MainCate"]);
                        item.SubCate = Convert.ToInt32(dr["SubCate"]);
                        List.Lst.Add(item);
                    }
                }
            }
            return new JavaScriptSerializer().Serialize(List);
        }

        protected void grvImg_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image oImg = (Image)e.Row.FindControl("imgItem");
                oImg.ImageUrl = "../Images/Flowers/" + DataBinder.Eval(e.Row.DataItem, "ImgPath");
                oImg.AlternateText = DataBinder.Eval(e.Row.DataItem, "Id").ToString();

                HyperLink linkImg = (HyperLink)e.Row.FindControl("linkImg");
                linkImg.NavigateUrl = "../Images/Flowers/" + DataBinder.Eval(e.Row.DataItem, "ImgPath");

                TextBox txtName = (TextBox)e.Row.FindControl("txtName");
                txtName.Text = DataBinder.Eval(e.Row.DataItem,"Name").ToString();

                TextBox txtDesc = (TextBox)e.Row.FindControl("txtDesc");
                txtDesc.Text = DataBinder.Eval(e.Row.DataItem, "Description").ToString();

                CheckBox ckShowHome = (CheckBox)e.Row.FindControl("ckShowHome");
                ckShowHome.Checked = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsShowHome"));

                CheckBox ckShowOwn = (CheckBox)e.Row.FindControl("ckShowOwn");
                ckShowOwn.Checked = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsShowOwn"));

                CheckBox ckActive = (CheckBox)e.Row.FindControl("ckActive");
                ckActive.Checked = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsActive"));

                CheckBox ckDelete = (CheckBox)e.Row.FindControl("ckDelete");
                ckDelete.Checked = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsDelete"));

                TextBox txtOrder = (TextBox)e.Row.FindControl("txtOrder");
                txtOrder.Text = DataBinder.Eval(e.Row.DataItem, "ImgOrder").ToString();

            }
        }


        protected void btnSubmitCate_Click(object sender, EventArgs e)
        {
            GetImage();
        }

        public void GetImage()
        {
            int iMainCate = CodeHelper.ToInt(ddlCate.SelectedValue);
            DataSet ds = new DataSet();
            DataTable dt=  new DataTable();
            int iSubCate = -1;
            string sName = string.Empty;
            BImage bImg;
            if (iMainCate != 1)
            {
                
                if (subCate.Visible == true)
                {
                    iSubCate = CodeHelper.ToInt(Request.Form["ctl00$Main$subCate"]);
                }
                sName = txtNameSerch.Text;
                bImg = new BImage();

                ds = bImg.GetImage(sName, iMainCate, iSubCate, -1, -1, -1, -1, false);
                if (ds != null)
                    dt = ds.Tables[0];
                
            }
            else
                dt = new BImage().GetAllImgShowHome(sName, iMainCate, iSubCate, -1, -1, -1, -1, false);

            if (dt.Rows.Count > 0)
                litJavas.Text = "<script type='text/javascript'>ShowUpdButton();</script>";
            else
                litJavas.Text = "<script type='text/javascript'>HiddenUpdButton();</script>";

            grvImg.DataSource = dt;
            grvImg.DataBind();
        }

        [WebMethod]
        public static string UpdateImg(List<CImage> lstImg)
        {
            string sDate = DateTime.Now.ToString();
            BImage bImg =  new BImage();
            bool bResult = false;
            foreach (CImage item in lstImg)
            {
                //bResult = bImg.UpdateImage(item.Id, item.Name, item.Description, item.ImgPath, item.ProductId, item.NewsId,
                //    item.CateId, item.SubCateId, sDate, item.ImgOrder, item.IsShowHome, item.IsShowOwn, item.IsActive, item.IsDelete);

                bResult = bImg.UpdateImage(item.Id, item.Name, item.Description, item.ImgPath, -1, -1,
                    -1, -1, sDate, item.ImgOrder, item.IsShowHome, item.IsShowOwn, item.IsActive, item.IsDelete);

            }
            if (bResult == false)
                return "false";
            return "true";
        }

        //protected void grvImg_PageIndexChanged(object sender, GridViewPageEventArgs  e)
        //{
        //    grvImg.PageIndex = e.NewPageIndex;
        //    GetImage();
        //}

        protected void grvImg_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvImg.PageIndex = e.NewPageIndex;
            GetImage();
        }

       
    }

    public class CategoryItem
    {
        public string CateId{ get; set; }
        public string Name { get; set; }
    }

    [Serializable]
    public class CategoryList
    {
        public List<CategoryItem> lst { get; set; }
    }
    [Serializable]
    public class ProductList
    {
        public List<CProduct> Lst
        {
            get;
            set;
        }
    }

}
